<template>
  <el-dialog :title="title" top="100px" width="600px" append-to-body :visible.sync="dialogedit" @open="openDialog"
    :before-close="closeDialog" :close-on-click-modal="false">
    <div class="form-box">
      <el-form ref="formdata" :model="ruleform" :rules="rules" label-suffix="：" :validate-on-rule-change="false"
        :show-message="false" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="数据标签" prop="dict_label"><el-input v-model="ruleform.dict_label"
                placeholder="请输入数据标签"></el-input></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据键值" prop="dict_value"><el-input v-model="ruleform.dict_value"
                placeholder="请输入数据键值"></el-input></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样式属性" prop="css_class"><el-input v-model="ruleform.css_class"
                placeholder="请输入样式属性"></el-input></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="回显样式" prop="list_class">
              <el-select v-model="ruleform.list_class" clearable>
                <el-option v-for="item in listClassOptions" :key="item.value"
                  :label="item.label + '(' + item.value + ')'" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :rows="5" v-model="ruleform.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="ruleform.status">
                <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="save_validate()">保 存</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import {
    saveData
  } from '@/api/system/dict/data';
  export default {
    name: 'dict_data_edit',
    dicts: ['sys_normal_disable'],
    props: {
      dialogedit: {
        type: Boolean,
        default: false
      },
      ruleform: {
        type: Object,
        default: () => {
          return {
            dict_type:'',
            dict_code: 0,
            dict_label: '',
            dict_value: '',
            css_class: '',
            list_class: '',
            status: '',
            remark: ''
          };
        }
      }
    },
    data() {
      return {
        title: '新增字典',
        rules: {
          dict_label: [{
            required: true,
            message: '数据标签不能为空'
          }],
          dict_value: [{
            required: true,
            message: '数据键值不能为空'
          }],
          status: [{
            required: true,
            message: '状态不能为空'
          }]
        },
        listClassOptions: [{
            value: "default",
            label: "默认"
          },
          {
            value: "primary",
            label: "主要"
          },
          {
            value: "success",
            label: "成功"
          },
          {
            value: "info",
            label: "信息"
          },
          {
            value: "warning",
            label: "警告"
          },
          {
            value: "danger",
            label: "危险"
          }
        ]
      };
    },
    created() {},
    methods: {
      closeDialog() {
        this.$nextTick(() => {
          this.$refs.formdata.resetFields();
          this.$emit('update:dialogedit', !this.dialogedit);
        });
      },
      save_validate() {
        this.$refs.formdata.validate((valid, valids) => {
          if (valid) {
            this.saveData();
          } else {
            var msg = '';
            for (var key in valids) {
              var item = valids[key];
              msg = item[0].message;
              break;
            }
            if (msg) this.$message.error(msg);

            return false;
          }
        });
      },

      async saveData() {
        const loading = this.loading('保存中...');
        saveData(this.ruleform).then(response => {
          this.$modal.msgSuccess("保存成功");
          loading.close();
          this.closeDialog();
          this.$emit('reload');
        }).catch(err => {
          loading.close();
        });
      },
      async openDialog() {
        if (this.ruleform.dict_code == 0) {
          this.title = "修改字典";
        } else {
          this.title = "新增字典";
        }
      }
    }
  };
</script>

<style scoped>
  .form-box {
    min-height: 60px;
    padding-right: 10px;
    overflow: auto;
  }
</style>
